// ElaScrollBarExample.cpp
#include <QApplication>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QCheckBox>
#include <QGroupBox>
#include <QSlider>
#include <QSpinBox>
#include <QTextEdit>
#include <QListWidget>
#include <QTableWidget>
#include <QTreeWidget>
#include <QScrollArea>

// ElaWidget 相关定义
#include "ElaWidget.h"
#include "ElaScrollBar.h"
#include "ElaApplication.h"


class ElaScrollBarExample : public ElaWidget
{
    Q_OBJECT

public:
    explicit ElaScrollBarExample(QWidget *parent = nullptr);
    ~ElaScrollBarExample();

private Q_SLOTS:
    void onAddListItem();
    void onClearList();
    void onScrollToTop();
    void onScrollToBottom();

private:
    void setupUI();
    void setupControlPanel();
    void setupDemoWidgets();
    void applyScrollBarSettings();
    QWidget* createControlPanel();
    QWidget *createTextEditGroup();
    QWidget *createListWidgetGroup();
    QWidget *createTableWidgetGroup();
    QWidget *createTreeWidgetGroup();
    QWidget *createScrollAreaGroup();
    QString createSampleText();

  private:
    // 控制面板组件
    QCheckBox *m_animationCheckBox;
    QCheckBox *m_expandEffectCheckBox;
    QSpinBox *m_speedLimitSpinBox;
    QSlider *m_opacitySlider;
    
    // 演示组件
    QTextEdit *m_textEdit;
    QListWidget *m_listWidget;
    QTableWidget *m_tableWidget;
    QTreeWidget *m_treeWidget;
    QScrollArea *m_scrollArea;
    QWidget *m_scrollContent;
    
    // ElaScrollBar 实例
    ElaScrollBar *m_customVScrollBar;
    ElaScrollBar *m_customHScrollBar;
};

ElaScrollBarExample::ElaScrollBarExample(QWidget *parent) 
    : ElaWidget(parent)
{
    setupUI();
}

ElaScrollBarExample::~ElaScrollBarExample()
{
}

void ElaScrollBarExample::setupUI()
{
    // 设置窗口标题和按钮
    setWindowTitle("ElaScrollBar 功能演示");
    setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | ElaAppBarType::MinimizeButtonHint | ElaAppBarType::MaximizeButtonHint | ElaAppBarType::CloseButtonHint | ElaAppBarType::ThemeChangeButtonHint | ElaAppBarType::NavigationButtonHint);

    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(10, 10, 10, 10);
    mainLayout->setSpacing(10);

    // 创建控制面板
    setupControlPanel();
    mainLayout->addWidget(createControlPanel());

    // 创建演示区域
    setupDemoWidgets();
    
    // 创建水平布局放置演示组件
    QHBoxLayout *demoLayout = new QHBoxLayout();
    demoLayout->setSpacing(15);
    
    // 左侧：文本和列表
    QVBoxLayout *leftLayout = new QVBoxLayout();
    leftLayout->addWidget(createTextEditGroup());
    leftLayout->addWidget(createListWidgetGroup());
    
    // 右侧：表格和树形
    QVBoxLayout *rightLayout = new QVBoxLayout();
    rightLayout->addWidget(createTableWidgetGroup());
    rightLayout->addWidget(createTreeWidgetGroup());
    rightLayout->addWidget(createScrollAreaGroup());
    
    demoLayout->addLayout(leftLayout);
    demoLayout->addLayout(rightLayout);
    
    mainLayout->addLayout(demoLayout);
    
    // 应用滚动条设置
    applyScrollBarSettings();
}

QWidget* ElaScrollBarExample::createControlPanel()
{
    QGroupBox *controlGroup = new QGroupBox("滚动条控制设置", this);
    QHBoxLayout *controlLayout = new QHBoxLayout(controlGroup);

    // 动画控制
    m_animationCheckBox = new QCheckBox("启用滚动动画", controlGroup);
    m_animationCheckBox->setChecked(true);

    // 悬停展开效果
    m_expandEffectCheckBox = new QCheckBox("悬停展开效果", controlGroup);
    m_expandEffectCheckBox->setChecked(true);

    // 速度限制
    QLabel *speedLabel = new QLabel("速度限制:", controlGroup);
    m_speedLimitSpinBox = new QSpinBox(controlGroup);
    m_speedLimitSpinBox->setRange(1, 100);
    m_speedLimitSpinBox->setValue(20);
    m_speedLimitSpinBox->setSuffix(" px");

    // 透明度
    QLabel *opacityLabel = new QLabel("滚动条透明度:", controlGroup);
    m_opacitySlider = new QSlider(Qt::Horizontal, controlGroup);
    m_opacitySlider->setRange(30, 100);
    m_opacitySlider->setValue(80);

    // 操作按钮
    QPushButton *addItemBtn = new QPushButton("添加列表项", controlGroup);
    QPushButton *clearListBtn = new QPushButton("清空列表", controlGroup);
    QPushButton *scrollTopBtn = new QPushButton("滚动到顶部", controlGroup);
    QPushButton *scrollBottomBtn = new QPushButton("滚动到底部", controlGroup);

    controlLayout->addWidget(m_animationCheckBox);
    controlLayout->addWidget(m_expandEffectCheckBox);
    controlLayout->addWidget(speedLabel);
    controlLayout->addWidget(m_speedLimitSpinBox);
    controlLayout->addWidget(opacityLabel);
    controlLayout->addWidget(m_opacitySlider);
    controlLayout->addWidget(addItemBtn);
    controlLayout->addWidget(clearListBtn);
    controlLayout->addWidget(scrollTopBtn);
    controlLayout->addWidget(scrollBottomBtn);
    controlLayout->addStretch();

    // 连接信号槽
    connect(m_animationCheckBox, &QCheckBox::toggled, this, &ElaScrollBarExample::applyScrollBarSettings);
    connect(m_expandEffectCheckBox, &QCheckBox::toggled, this, &ElaScrollBarExample::applyScrollBarSettings);
    connect(m_speedLimitSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &ElaScrollBarExample::applyScrollBarSettings);
    connect(m_opacitySlider, &QSlider::valueChanged, this, &ElaScrollBarExample::applyScrollBarSettings);
    
    connect(addItemBtn, &QPushButton::clicked, this, &ElaScrollBarExample::onAddListItem);
    connect(clearListBtn, &QPushButton::clicked, this, &ElaScrollBarExample::onClearList);
    connect(scrollTopBtn, &QPushButton::clicked, this, &ElaScrollBarExample::onScrollToTop);
    connect(scrollBottomBtn, &QPushButton::clicked, this, &ElaScrollBarExample::onScrollToBottom);

    return controlGroup;
}

QWidget* ElaScrollBarExample::createTextEditGroup()
{
    QGroupBox *group = new QGroupBox("文本编辑器 (自定义滚动条)", this);
    QVBoxLayout *layout = new QVBoxLayout(group);
    
    m_textEdit = new QTextEdit(group);
    m_textEdit->setPlainText(createSampleText());
    
    // 替换默认滚动条为 ElaScrollBar
    m_customVScrollBar = new ElaScrollBar(m_textEdit->verticalScrollBar(), m_textEdit);
    
    layout->addWidget(m_textEdit);
    return group;
}

QWidget* ElaScrollBarExample::createListWidgetGroup()
{
    QGroupBox *group = new QGroupBox("列表控件", this);
    QVBoxLayout *layout = new QVBoxLayout(group);
    
    m_listWidget = new QListWidget(group);
    for (int i = 1; i <= 50; ++i) {
        m_listWidget->addItem(QString("列表项 %1 - 这是自定义滚动条的演示").arg(i));
    }
    
    // 替换默认滚动条
    new ElaScrollBar(m_listWidget->verticalScrollBar(), m_listWidget);
    
    layout->addWidget(m_listWidget);
    return group;
}

QWidget* ElaScrollBarExample::createTableWidgetGroup()
{
    QGroupBox *group = new QGroupBox("表格控件", this);
    QVBoxLayout *layout = new QVBoxLayout(group);
    
    m_tableWidget = new QTableWidget(100, 5, group);
    QStringList headers;
    headers << "列1" << "列2" << "列3" << "列4" << "列5";
    m_tableWidget->setHorizontalHeaderLabels(headers);
    
    // 填充数据
    for (int row = 0; row < 100; ++row) {
        for (int col = 0; col < 5; ++col) {
            QTableWidgetItem *item = new QTableWidgetItem(
                QString("数据 %1-%2").arg(row + 1).arg(col + 1));
            m_tableWidget->setItem(row, col, item);
        }
    }
    
    // 替换滚动条
    new ElaScrollBar(m_tableWidget->verticalScrollBar(), m_tableWidget);
    new ElaScrollBar(m_tableWidget->horizontalScrollBar(), m_tableWidget);
    
    layout->addWidget(m_tableWidget);
    return group;
}

QWidget* ElaScrollBarExample::createTreeWidgetGroup()
{
    QGroupBox *group = new QGroupBox("树形控件", this);
    QVBoxLayout *layout = new QVBoxLayout(group);
    
    m_treeWidget = new QTreeWidget(group);
    m_treeWidget->setHeaderLabels(QStringList() << "名称" << "类型" << "大小");
    
    // 添加树形数据
    for (int i = 1; i <= 20; ++i) {
        QTreeWidgetItem *rootItem = new QTreeWidgetItem(
            QStringList() << QString("根节点 %1").arg(i) << "文件夹" << "");
        
        for (int j = 1; j <= 10; ++j) {
            QTreeWidgetItem *childItem = new QTreeWidgetItem(
                QStringList() << QString("子节点 %1-%2").arg(i).arg(j) << "文件" << "100 KB");
            rootItem->addChild(childItem);
        }
        
        m_treeWidget->addTopLevelItem(rootItem);
        rootItem->setExpanded(true);
    }
    
    // 替换滚动条
    new ElaScrollBar(m_treeWidget->verticalScrollBar(), m_treeWidget);
    
    layout->addWidget(m_treeWidget);
    return group;
}

QWidget* ElaScrollBarExample::createScrollAreaGroup()
{
    QGroupBox *group = new QGroupBox("滚动区域", this);
    QVBoxLayout *layout = new QVBoxLayout(group);
    
    m_scrollArea = new QScrollArea(group);
    m_scrollContent = new QWidget();
    QVBoxLayout *contentLayout = new QVBoxLayout(m_scrollContent);
    
    // 添加大量内容到滚动区域
    for (int i = 1; i <= 50; ++i) {
        QLabel *label = new QLabel(
            QString("滚动区域内容项 %1 - 这是一个很长的文本用于演示滚动条的效果和性能").arg(i), 
            m_scrollContent);
        label->setWordWrap(true);
        label->setStyleSheet("QLabel { padding: 8px; border: 1px solid #ddd; margin: 2px; }");
        contentLayout->addWidget(label);
    }
    contentLayout->addStretch();
    
    m_scrollArea->setWidget(m_scrollContent);
    m_scrollArea->setWidgetResizable(true);
    
    // 替换滚动条
    new ElaScrollBar(m_scrollArea->verticalScrollBar(), m_scrollArea);
    new ElaScrollBar(m_scrollArea->horizontalScrollBar(), m_scrollArea);
    
    layout->addWidget(m_scrollArea);
    return group;
}

void ElaScrollBarExample::setupControlPanel()
{
    // 已在 createControlPanel() 中实现
}

void ElaScrollBarExample::setupDemoWidgets()
{
    // 已在各个创建函数中实现
}

void ElaScrollBarExample::applyScrollBarSettings()
{
    // 获取所有 ElaScrollBar 实例并应用设置
    QList<ElaScrollBar*> scrollBars = findChildren<ElaScrollBar*>();
    
    for (ElaScrollBar *scrollBar : scrollBars) {
        scrollBar->setIsAnimation(m_animationCheckBox->isChecked());
        scrollBar->setSpeedLimit(m_speedLimitSpinBox->value());
        
        // 注意：悬停展开效果和透明度设置可能需要通过样式表或自定义属性实现
        // 这里假设 ElaScrollBar 有相应的方法
        // scrollBar->setExpandOnHover(m_expandEffectCheckBox->isChecked());
        // scrollBar->setOpacity(m_opacitySlider->value() / 100.0);
    }
}

QString ElaScrollBarExample::createSampleText()
{
    return QString(
        "ElaScrollBar 功能演示\n\n"
        "特性介绍：\n"
        "• 平滑滚动动画\n"
        "• 自定义滚动速度限制\n"
        "• 悬停展开效果\n"
        "• 右键上下文菜单\n"
        "• 高性能滚动\n"
        "• 主题支持\n\n"
        "使用说明：\n"
        "1. 使用鼠标滚轮进行快速滚动\n"
        "2. 右键点击滚动条查看上下文菜单\n"
        "3. 调整控制面板参数实时预览效果\n"
        "4. 测试各种控件的滚动行为\n\n"
        "%1\n\n").arg(QString("示例文本行\n").repeated(100));
}

void ElaScrollBarExample::onAddListItem()
{
    int count = m_listWidget->count();
    m_listWidget->addItem(QString("新添加的列表项 %1").arg(count + 1));
}

void ElaScrollBarExample::onClearList()
{
    m_listWidget->clear();
}

void ElaScrollBarExample::onScrollToTop()
{
    m_listWidget->scrollToTop();
    m_textEdit->moveCursor(QTextCursor::Start);
    m_tableWidget->scrollToTop();
    m_treeWidget->scrollToTop();
    m_scrollArea->verticalScrollBar()->setValue(0);
}

void ElaScrollBarExample::onScrollToBottom()
{
    m_listWidget->scrollToBottom();
    m_textEdit->moveCursor(QTextCursor::End);
    m_tableWidget->scrollToBottom();
    m_treeWidget->scrollToBottom();
    m_scrollArea->verticalScrollBar()->setValue(m_scrollArea->verticalScrollBar()->maximum());
}

// 主函数
int main(int argc, char *argv[])
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#else
    qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif

    QApplication app(argc, argv);
    eApp->init();
    
    // 设置应用程序样式
    app.setStyle("Fusion");

    // 创建示例窗口
    ElaScrollBarExample window;
    window.resize(1200, 800);
    window.moveToCenter();
    window.show();

    return app.exec();
}

#include "ElaScrollBarTest.moc"